﻿using System;
using System.Collections.Generic;
using Hammock.Web;

namespace WeiboPlus.Models
{
    public class SNSRequest
    {
        public SNSRequest()
        {
            Parameters = new List<SNSParameter>();
            WebMethod = WebMethod.Get;
        }

        WebMethod _webMethod;
        public WebMethod WebMethod
        {
            get
            {
                return _webMethod;
            }
            set
            {
                _webMethod = value;
            }
        }

        List<SNSParameter> _parameters;
        public List<SNSParameter> Parameters
        {
            get
            {
                return _parameters;
            }
            set
            {
                _parameters = value;
            }
        }

        Func<SNSParameter, bool> _predicate;
        public Func<SNSParameter, bool> Predicate
        {
            get
            {
                return _predicate;
            }
            set
            {
                _predicate = value;
            }
        }

        SNSType _type;
        public SNSType Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }

        string _method;
        public string Method
        {
            get
            {
                return _method;
            }
            set
            {
                _method = value;
            }
        }

        SNSStatus _status;

        public SNSStatus Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public SNSRequest(IEnumerable<SNSParameter> parameters)
            : this()
        {
            foreach (var item in parameters)
            {
                Parameters.Add(new SNSParameter(item.Name, item.Value));
            }
        }
    }

    public enum ParameterStyle
    {
        Normal, Restful, InHeader,
        Post,
        File,
        FilePath,
        Bytes
    }

    public class SNSParameter
    {
        public SNSParameter(string name, string value)
        {
            Name = name;
            Value = value;
        }

        string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        string _value;
        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }

        ParameterStyle _style;
        public ParameterStyle Style
        {
            get
            {
                return _style;
            }
            set
            {
                _style = value;
            }
        }
    }

    public class SNSFileParameter:SNSParameter
    {
        public SNSFileParameter(string name, string path, byte[] value)
            : base(name, path)
        {
            Name = name;
            Value = path;
            Style = ParameterStyle.Bytes;
            Bytes = value;
        }

        byte[] bytes;
        public byte[] Bytes
        {
            get
            {
                return bytes;
            }
            set
            {
                bytes = value;
            }
        }
    }

    /// <summary>
    /// Comparer class used to perform the sorting of the query parameters
    /// </summary>
    public class ParameterComparer : IComparer<SNSParameter>
    {
        public int Compare(SNSParameter x, SNSParameter y)
        {
            if (x.Name == y.Name)
            {
                return string.Compare(x.Value, y.Value);
            }
            else
            {
                return string.Compare(x.Name, y.Name);
            }
        }
    }
}
